# -*-coding:utf-8-*-

from PyQt5.QtWidgets import QMainWindow, QApplication, QRadioButton, QGroupBox, QVBoxLayout, QWidget, QHBoxLayout, \
    QButtonGroup, QLabel

import sys


class RadioRuttonDemo(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        self.statusBar()

        language_gb = QGroupBox("请选择您喜欢的语言：")  # 定义一个容器

        vbllayout = QVBoxLayout()

        self.python_rb = QRadioButton("Python", language_gb)

        vbllayout.addWidget(self.python_rb, 11)

        self.cplus_rb = QRadioButton("C++", language_gb)

        vbllayout.addWidget(self.cplus_rb, 12)

        self.vb_rb = QRadioButton("Visual Basic", language_gb)

        vbllayout.addWidget(self.vb_rb, 13)

        self.java_rb = QRadioButton("Java", language_gb)

        vbllayout.addWidget(self.java_rb, 14)

        language_gb.setLayout(vbllayout)  # 定义GroupBox的布局管理器

        self.languagelist = [self.python_rb, self.cplus_rb, self.vb_rb, self.java_rb]

        self.java_rb.setChecked(True)  # 设置默认的选项

        self.languageconnect()  # 调用函数

        lb1 = QLabel("请选择您喜欢的球类运动：")  # 用标签显示提示信息

        self.bg_ball = QButtonGroup()  # 创建一个按钮组

        self.baseketball_rb = QRadioButton("篮球", self)

        self.volleyball_rb = QRadioButton("排球", self)

        self.football_rb = QRadioButton("足球", self)

        self.badminton_rb = QRadioButton("羽毛球", self)

        self.football_rb.setChecked(True)  # 设置默认选中项

        self.bg_ball.addButton(self.baseketball_rb, 21)

        self.bg_ball.addButton(self.volleyball_rb, 22)

        self.bg_ball.addButton(self.football_rb, 23)

        self.bg_ball.addButton(self.badminton_rb, 24)

        hblayout = QHBoxLayout()  # 设置四个选项的布局管理

        hblayout.addWidget(self.baseketball_rb)

        hblayout.addWidget(self.volleyball_rb)

        hblayout.addWidget(self.football_rb)

        hblayout.addWidget(self.badminton_rb)

        vlayout = QVBoxLayout()

        vlayout.addWidget(lb1)

        vlayout.addLayout(hblayout)

        self.bg_ball.buttonClicked.connect(self.balldo)  # 按钮组事件与函数关联

        mainvblayout = QVBoxLayout()

        mainvblayout.addWidget(language_gb)

        mainvblayout.addLayout(vlayout)

        main_frame = QWidget()  # 此窗格作为以上诸控件的容器
        main_frame.setLayout(mainvblayout)

        self.setCentralWidget(main_frame)  # 将窗格放在mainwindow的中间

        self.statusBar()

        self.setGeometry(100, 100, 300, 250)

        self.setWindowTitle("单选按钮示例")

        self.show()

    def languageconnect(self):

        for rb in self.languagelist:
            rb.clicked.connect(self.languagedo)

    def languagedo(self):

        sender = self.sender()
        if sender == self.python_rb:

            self.statusBar().showMessage('Python')

        elif sender == self.cplus_rb:

            self.statusBar().showMessage('C++')

        elif sender == self.vb_rb:

            self.statusBar().showMessage('Visual Basic')

        elif sender == self.java_rb:

            self.statusBar().showMessage('Java')

    def balldo(self):

        if self.bg_ball.checkedId() == 21:

            self.statusBar().showMessage('你喜欢玩篮球')

        elif self.bg_ball.checkedId() == 22:

            self.statusBar().showMessage('你喜欢玩排球')

        elif self.bg_ball.checkedId() == 23:

            self.statusBar().showMessage('你喜欢踢足球')

        elif self.bg_ball.checkedId() == 24:

            self.statusBar().showMessage('你喜欢打羽毛球')


if __name__ == "__main__":
    app = QApplication(sys.argv)
    rb_w = RadioRuttonDemo()
    sys.exit(app.exec_())
